#include <my_header.h>
#include <string.h>

#define FILE1_PATH "1.txt"
#define FILE2_PATH "2.txt"
#define BUF_SIZE 4096

/* Usage:  */
int main(int argc, char *argv[]){
    char sendbuf[BUF_SIZE] = {0};
    char recvbuf[BUF_SIZE] = {0};

    while(1){
        // A recv input
        printf("A input > \n");
        ssize_t read_count1 = read(STDIN_FILENO, sendbuf, sizeof(sendbuf) - 1);
        if(read_count1 <= 0){
            break;
        }
        sendbuf[read_count1] = '\0';

        // A write 1.txt
        int fd1 = open(FILE1_PATH, O_WRONLY | O_CREAT | O_TRUNC, 0664);
        if(fd1 > 0){
            write(fd1, sendbuf, read_count1);
            close(fd1);
        }

        // A recv 2.txt
        int fd2 = open(FILE2_PATH, O_RDONLY);
        if(fd2 > 0){
            ssize_t read_count2 = read(fd2, recvbuf, sizeof(recvbuf) - 1);
            if(read_count2 > 0){
                recvbuf[read_count2] = '\0';
                printf("B send > %s \n", recvbuf);
                close(fd2);
            }
        }

        // A sleep
        sleep(1);
    }
    return 0;
}

